package mi.payment.settings;

import mi.payment.utils.SharedPreferencesUtil;

/**
 * 离线交易控制数据类
 *
 * @author lj on 2017/2/16.
 */
public class OfflineTransactionOperation {

    private static OfflineTransactionOperation instance;
    private OfflineTransactionSettings instanceSettings;

    private OfflineTransactionOperation() {
        instanceSettings = getOfflineTransactionSettings();
        if (instanceSettings == null) {
            instanceSettings = new OfflineTransactionSettings();
        }
        saveOfflineTransactionOperation(instanceSettings);
    }

    public static OfflineTransactionOperation getInstance() {
        if (instance == null) {
            instance = new OfflineTransactionOperation();
        }
        return instance;
    }

    public OfflineTransactionSettings getOfflineTransactionSettings() {
        if(instanceSettings == null) {
            Object obj = SharedPreferencesUtil.readObj(OfflineTransactionSettings.OFFLINE_TRANSACTION_KEY);
            if (obj instanceof OfflineTransactionSettings) {
                instanceSettings = (OfflineTransactionSettings) obj;
            } else {
                instanceSettings = new OfflineTransactionSettings();
                saveOfflineTransactionOperation(instanceSettings);
            }
        }
        return instanceSettings;
    }

    public void init(int offlinePushCount, int automaticallyNumber, boolean smallPaymentWay) {
        OfflineTransactionSettings bean = getOfflineTransactionSettings();
        bean.setOfflinePushCount(offlinePushCount);
        bean.setAutomaticallyNumber(automaticallyNumber);
        bean.setSmallPaymentWay(smallPaymentWay);
        saveOfflineTransactionOperation(bean);
    }

    public void saveOfflineTransactionOperation(OfflineTransactionSettings offlineTransactionSettings) {
        this.instanceSettings = offlineTransactionSettings;
        SharedPreferencesUtil.saveObj(offlineTransactionSettings, OfflineTransactionSettings.OFFLINE_TRANSACTION_KEY);
    }

    /**
     * 离线上送次数
     *
     * @param offlinePushCount 离线上送次数
     */
    public void saveOfflinePushCount(int offlinePushCount) {
        OfflineTransactionSettings offline = getOfflineTransactionSettings();
        offline.setOfflinePushCount(offlinePushCount);
        saveOfflineTransactionOperation(offline);
    }

    public void saveAutomaticallyNumber(int automaticallyNumber) {
        OfflineTransactionSettings offline = getOfflineTransactionSettings();
        offline.setAutomaticallyNumber(automaticallyNumber);
        saveOfflineTransactionOperation(offline);
    }

    public void saveSmallPaymentWay(boolean smallPaymentWay) {
        OfflineTransactionSettings offline = getOfflineTransactionSettings();
        offline.setSmallPaymentWay(smallPaymentWay);
        saveOfflineTransactionOperation(offline);
    }

    /**
     * 清除离线交易控制的存储
     */
    public void clearOfflineTransaction() {
        SharedPreferencesUtil.clearByKey(OfflineTransactionSettings.OFFLINE_TRANSACTION_KEY);
    }
}
